package com.bookstore.back.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bookstore.back.entity.UserFavorite;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 用户收藏Mapper接口
 * 
 * @author 程序猿_Ti
 * @since 2025-07-22
 */
@Mapper
public interface UserFavoriteMapper extends BaseMapper<UserFavorite> {

    /**
     * 获取用户收藏列表（包含图书详细信息）
     */
    @Select("SELECT uf.id, uf.user_id, uf.book_id, uf.created_at, " +
            "b.title, b.author, b.publisher, b.price, b.original_price, " +
            "b.cover, b.description, b.rating, b.stock, b.status " +
            "FROM user_favorite uf " +
            "LEFT JOIN book b ON uf.book_id = b.id " +
            "WHERE uf.user_id = #{userId} " +
            "ORDER BY uf.created_at DESC")
    List<Map<String, Object>> getUserFavoriteList(@Param("userId") Integer userId);

    /**
     * 分页获取用户收藏列表（包含图书详细信息）
     */
    @Select("SELECT uf.id, uf.user_id, uf.book_id, uf.created_at, " +
            "b.title, b.author, b.publisher, b.price, b.original_price, " +
            "b.cover, b.description, b.rating, b.stock, b.status " +
            "FROM user_favorite uf " +
            "LEFT JOIN book b ON uf.book_id = b.id " +
            "WHERE uf.user_id = #{userId} " +
            "ORDER BY uf.created_at DESC")
    IPage<Map<String, Object>> getUserFavoriteListPage(Page<Map<String, Object>> page, @Param("userId") Integer userId);

    /**
     * 检查用户是否收藏了某本图书
     */
    @Select("SELECT COUNT(*) FROM user_favorite WHERE user_id = #{userId} AND book_id = #{bookId}")
    int checkUserFavorite(@Param("userId") Integer userId, @Param("bookId") Integer bookId);

    /**
     * 获取用户收藏数量
     */
    @Select("SELECT COUNT(*) FROM user_favorite WHERE user_id = #{userId}")
    int getUserFavoriteCount(@Param("userId") Integer userId);
}
